package com.xwn.utils.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * 时间操作
 * @author lifengyao
 *
 */
public class MyDateUtil {
	
	/**
	 * 将毫秒值转成字符串值
	 */
	public static String getDateStrForYYYYMMDDHHMMSS(long mile){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date date2 = new Date();
		date2.setTime(mile);
		return simpleDateFormat.format(date2);
	}
	
	/**
	 * 当前时间转换为format格式时间
	 * @param format
	 * @return
	 */
	public static String getDateStrFormat(String format){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date2 = new Date();
		return simpleDateFormat.format(date2);
	}
	
	/**
	 * 将毫秒值转成字符串值
	 * @throws ParseException 
	 */
	public static long getLongTimeByYYYYMMDDHHMMSS(String datetime) throws ParseException{
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date date2 = simpleDateFormat.parse(datetime);
		
		return date2.getTime();
	}
	
	/**
	 * 将old_exe格式字符串          转成           新的格式new_exe的字符串值
	 * @throws ParseException 
	 */
	public static String getNewDateByXEX(String old_exe,String datetime,String new_exe) throws ParseException{
//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(old_exe);
		Date date2 = simpleDateFormat.parse(datetime);
		
		SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(new_exe);
//		SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy.MM.dd mm:ss");
		return simpleDateFormat2.format(date2);
	}
	
	/** 
	 *  
	 * @param 要转换的毫秒数 
	 * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式 
	 * @author fy.zhang 
	 */  
	public static String formatDuring(long mss) {  
	    long days = mss / (1000 * 60 * 60 * 24);  
	    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);  
//	    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);  
//	    long seconds = (mss % (1000 * 60)) / 1000;  
	    return days + "天 " + hours + "小时";
	}
	
	/**
	 * 获得当天零时零分零秒
	 * @return 返回Date
	 */
	public static Date getStartTimeForNow(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 获得当天23时59分59秒
	 * @return 返回Date
	 */
	public static Date getEndTimeForNow() {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	public static String getDateByParam(Date date, String string) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static void main(String[] args) throws ParseException {
		System.out.println(getLongTimeByYYYYMMDDHHMMSS("20190114000000000"));
//		System.out.println(getStartTimeForNow());
//		System.out.println(getEndTimeForNow());
//		System.out.println(formatDuring(197471674));
//		System.out.println(DateUtils.getNowDate());
//		System.out.println(getLongTimeByYYYYMMDDHHMMSS("20180902145137054"));
//		System.out.println(DateUtils.getNowDateByYYYYMMDD());
//		System.out.println(MyDateUtil.getNewDateByXEX("yyyyMMddHHmmssSSS", DateUtils.getNowDate(), "yyyy-MM-dd"));
//		System.out.println(DateUtils.getNowDate()+"--"+DateUtils.getNowDate().substring(4, 6));
//		System.out.println(getDateStrForYYYYMMDDHHMMSS(20181230000000000));
	}
}
